package com.dragon.logical.service.job;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.dragon.common.constant.CronJobConstant;
import com.dragon.common.util.CovertUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
public class CalendarUpdateJob extends QuartzJobBean {

    private final static String DATE_API_URL = "https://opendata.baidu.com/data/inner";


    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("执行日历cron定时任务{}", LocalDateTime.now());
        String param = String.valueOf(context.getTrigger().getJobDataMap().get(CronJobConstant.REMINDER_JOB_DATA_MAP.variable()));
        calendarUpdate(param);
    }


    //获取百度万年历数据
    private void calendarUpdate(String param){
        Map<String,String> map = JSON.parseObject(param, Map.class);
        Integer years = Integer.valueOf(map.get("years"));
        List<Integer> months = Arrays.asList(2,5,8,11);
        Map<String,Object> params = new HashMap<>();
        params.put("tn","reserved_all_res_tn");
        params.put("type","json");
        params.put("resource_id","52109");
        params.put("apiType","yearMonthData");
        params.put("cb","json");
        months.forEach(months1 ->{
            String query = String.valueOf(years).concat("年");
            query.concat(String.valueOf(months1)).concat("月");
            params.put("query",query);
            String Result = HttpUtil.get(DATE_API_URL,params);
        });
    }
}
